package YAPserver.gameObjects;

import YAPserver.collisionEngine.Wall;
import YAPserver.logic.Output;

/**
 * An abstract class for all gameObjects that are line-based. 
 * @author Daniel
 *
 */
public abstract class AbstractWall extends AbstractGameObject implements Wall{
	
	protected Point m_startPoint;
	protected Point m_endPoint;
	protected Point m_normal;
	protected Output m_out;
	
	/**
	 * Constructs a wallAbstract using two points
	 * @param startPoint point used to construct wallAbstract
	 * @param endPoint point used to construct wallAbstract
	 */
	public AbstractWall(Point startPoint, Point endPoint){
		m_startPoint = startPoint;
		m_endPoint = endPoint;
		m_middlePoint = new Point(0, 0);
		calcMiddlePoint(m_startPoint, m_endPoint);
		m_line = new Line(m_startPoint, m_endPoint);
		calcNormal();
	}
	
	/**
	 * Constructs a wallAbstract with the same line as the specified wall.
	 * @param wall wall to be copied
	 */
	public AbstractWall(GameWall wall) {
		m_startPoint = new Point(wall.getStartPoint());
		m_endPoint = new Point(wall.getEndPoint());
		m_middlePoint = new Point(wall.getMiddlePoint());
		m_line = new Line(m_startPoint, m_endPoint);
		calcNormal();
	}

	protected void calcMiddlePoint(Point point1, Point point2){
		//simple vector calculation 1/2*a + 1/2*b = c
		double x = 1./2.*point1.getXPos()+1./2.*point2.getXPos();
		double y = 1./2.*point1.getYPos()+1./2.*point2.getYPos();
		m_middlePoint.setXYPos(x, y);
	}
	
	private void calcNormal(){
		double x = -m_endPoint.subtract(m_startPoint).getYPos();
		double y = m_endPoint.subtract(m_startPoint).getXPos();
		double length = m_line.getLength();
		m_normal = new Point(x/length, y/length);
	}
	
//	public double angleBetweenWalls(Wall wall){
//		return this.m_line.realAngleBetweenLines(wall.getLine());
//	}

	/**
	 * Returns the starting point of this wallAbstract.
	 * @return the starting point of this wallAbstract
	 */
	public Point getStartPoint() {
		return m_line.getStartPoint();
	}

	/**
	 * Returns the end point of this wallAbstract.
	 * @return the end point of this wallAbstract
	 */
	public Point getEndPoint() {
		return m_line.getEndPoint();
	}
	
	/**
	 * Returns the normal vector of this wallAbstract in form of a point (coordinate vector).
	 * @return the normal vector of this wallAbstract
	 * @see Point
	 */
	public Point getNormal(){
		return m_normal;
	}
	
	/**
	 * Sets the output for this wall. The output is only used to send sounds.
	 * @param out output used for sending sounds
	 */
	public void setOutput(Output out){
		m_out = out;
	}
	@Override
	public String toString(){
		String s = "";
		s = s + m_line;
		return s;
	}
}

